import 'package:flutter/material.dart';

class Utils {
  Utils._();
  static bool isEmpty(String? s) {
    if (null == s || s.isEmpty) {
      return true;
    }
    for (var c in s.split('')) {
      if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f')) {
        return false;
      }
    }
    return true;
  }

  static void showInputDialog(
    BuildContext context,
    String title,
    Function(String) cb,
  ) {
    final textController = TextEditingController();
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          content: TextField(
            controller: textController,
            decoration: const InputDecoration(hintText: ''),
            autofocus: true,
          ),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
            TextButton(
              onPressed: () {
                final text = textController.text;
                if (text.isNotEmpty) {
                  Navigator.of(context).pop();
                  cb(text);
                }
              },
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
  }

  static void showConfirmDialog(
    BuildContext context,
    String title,
    Function() cb,
  ) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(title),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
                cb();
              },
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
  }

  static void showToast(BuildContext context, String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message), duration: Duration(seconds: 2)),
    );
  }
}
